package cn.ytr.crm.web.controller;

import cn.ytr.crm.domain.Customer;
import cn.ytr.crm.query.CustomerQueryObject;
import cn.ytr.crm.service.ICustomerService;
import cn.ytr.crm.service.IEmployeeService;
import cn.ytr.crm.service.ISystemDictionaryItemService;
import cn.ytr.crm.util.JsonResult;
import cn.ytr.crm.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/customer")
@Controller
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    /**
     * 显示 客户列表页
     */
    @RequiresPermissions(value = {"访问:客户列表页面", "customer:list"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //用分页插件
        model.addAttribute("pageInfo", customerService.query(qo));
        return "customer/list";
    }

    /**
     * 显示 潜在客户列表页面
     */
    @RequiresPermissions(value = {"访问:潜在客户列表页面", "customer:potentialList"}, logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //只查询查询潜在客户的
        qo.setStatus(Customer.STATUS_COMMON);

        //判断角色管理员和经理，如果不是管理员和经理，就只能看到自己的潜在客户，管理员和经理则可以看到所有的潜在客户
        Subject subject = SecurityUtils.getSubject();
        if ( !(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager")) ) {
            //查访问主体的数据
            qo.setSellerId(UserContext.getCurrentUserByShiro().getId());
        }

        //市场专员下拉框的数据共享(市场专员职位是 Market_Manager 和 Market)(页面)
        model.addAttribute("sellers", employeeService.selectByRoleSn("Market_Manager", "Market"));
        //查询职业下拉框的数据共享(新增或编辑客户模态框 客户工作)
        model.addAttribute("jobs", systemDictionaryItemService.selectByDicSn("job"));
        //查询来源下拉框的数据共享(新增或编辑客户模态框 客户来源)
        model.addAttribute("sources", systemDictionaryItemService.selectByDicSn("source"));
        //查询交流方式下拉框数据(跟进 模态框 交流方式)
        model.addAttribute("communicationMethod",systemDictionaryItemService.selectByDicSn("communicationMethod"));

        model.addAttribute("pageInfo", customerService.query(qo));
        return "customer/potential_list";
    }

    /**
     * 显示 正式客户列表页面
     */
    @RequiresPermissions(value = {"访问:正式客户列表页面", "customer:normalList"}, logical = Logical.OR)
    @RequestMapping("/normalList")
    public String normalList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //只查询查询潜在客户的
        qo.setStatus(Customer.STATUS_NORMAL);

        //判断角色管理员和经理，如果不是管理员和经理，就只能看到自己的潜在客户，管理员和经理则可以看到所有的潜在客户
        Subject subject = SecurityUtils.getSubject();
        if ( !(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager")) ) {
            //查访问主体的数据
            qo.setSellerId(UserContext.getCurrentUserByShiro().getId());
        }

        //市场专员下拉框的数据共享(市场专员职位是 Market_Manager 和 Market)(页面)
        model.addAttribute("sellers", employeeService.selectByRoleSn("Market_Manager", "Market"));
        //查询职业下拉框的数据共享(新增或编辑客户模态框 客户工作)
        model.addAttribute("jobs", systemDictionaryItemService.selectByDicSn("job"));
        //查询来源下拉框的数据共享(新增或编辑客户模态框 客户来源)
        model.addAttribute("sources", systemDictionaryItemService.selectByDicSn("source"));
        //查询交流方式下拉框数据(跟进 模态框 交流方式)
        model.addAttribute("communicationMethod",systemDictionaryItemService.selectByDicSn("communicationMethod"));

        model.addAttribute("pageInfo", customerService.query(qo));
        return "customer/normal_list";
    }

    /**
     * 显示 客户池列表页面
     */
    @RequiresPermissions(value = {"访问:客户池列表页面", "customer:poolList"}, logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //只查询查询客户池的
        qo.setStatus(Customer.STATUS_POOL);

        Subject subject = SecurityUtils.getSubject();
        /*//判断角色管理员和经理，如果不是管理员和经理，就只能看到自己的潜在客户，管理员和经理则可以看到所有的潜在客户
        if ( !(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager")) ) {
            //查访问主体的数据
            qo.setSellerId(UserContext.getCurrentUserByShiro().getId());
        }*/

        //市场专员下拉框的数据共享(市场专员职位是 Market_Manager 和 Market)(页面)
        model.addAttribute("sellers", employeeService.selectByRoleSn("Market_Manager", "Market"));
        //查询职业下拉框的数据共享(新增或编辑客户模态框 客户工作)
        model.addAttribute("jobs", systemDictionaryItemService.selectByDicSn("job"));
        //查询来源下拉框的数据共享(新增或编辑客户模态框 客户来源)
        model.addAttribute("sources", systemDictionaryItemService.selectByDicSn("source"));
        //查询交流方式下拉框数据(跟进 模态框 交流方式)
        model.addAttribute("communicationMethod",systemDictionaryItemService.selectByDicSn("communicationMethod"));

        model.addAttribute("pageInfo", customerService.query(qo));
        return "customer/pool_list";
    }

    /**
     * 显示 失败客户列表页面
     */
    @RequiresPermissions(value = {"访问:失败客户列表页面", "customer:failList"}, logical = Logical.OR)
    @RequestMapping("/failList")
    public String failList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //只查询失败的
        qo.setStatus(Customer.STATUS_FALL);

        Subject subject = SecurityUtils.getSubject();
        /*//判断角色管理员和经理，如果不是管理员和经理，就只能看到自己的潜在客户，管理员和经理则可以看到所有的潜在客户
        if ( !(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager")) ) {
            //查访问主体的数据
            qo.setSellerId(UserContext.getCurrentUserByShiro().getId());
        }*/

        //市场专员下拉框的数据共享(市场专员职位是 Market_Manager 和 Market)(页面)
        model.addAttribute("sellers", employeeService.selectByRoleSn("Market_Manager", "Market"));
        //查询职业下拉框的数据共享(新增或编辑客户模态框 客户工作)
        model.addAttribute("jobs", systemDictionaryItemService.selectByDicSn("job"));
        //查询来源下拉框的数据共享(新增或编辑客户模态框 客户来源)
        model.addAttribute("sources", systemDictionaryItemService.selectByDicSn("source"));
        //查询交流方式下拉框数据(跟进 模态框 交流方式)
        model.addAttribute("communicationMethod",systemDictionaryItemService.selectByDicSn("communicationMethod"));

        model.addAttribute("pageInfo", customerService.query(qo));
        return "customer/fail_list";
    }

    /**
     * 显示 流失客户列表页面
     */
    @RequiresPermissions(value = {"访问:流失客户列表页面", "customer:lostList"}, logical = Logical.OR)
    @RequestMapping("/lostList")
    public String lostList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //只查询查询流失的
        qo.setStatus(Customer.STATUS_LOST);

        Subject subject = SecurityUtils.getSubject();
        /*//判断角色管理员和经理，如果不是管理员和经理，就只能看到自己的潜在客户，管理员和经理则可以看到所有的潜在客户
        if ( !(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager")) ) {
            //查访问主体的数据
            qo.setSellerId(UserContext.getCurrentUserByShiro().getId());
        }*/

        //市场专员下拉框的数据共享(市场专员职位是 Market_Manager 和 Market)(页面)
        model.addAttribute("sellers", employeeService.selectByRoleSn("Market_Manager", "Market"));
        //查询职业下拉框的数据共享(新增或编辑客户模态框 客户工作)
        model.addAttribute("jobs", systemDictionaryItemService.selectByDicSn("job"));
        //查询来源下拉框的数据共享(新增或编辑客户模态框 客户来源)
        model.addAttribute("sources", systemDictionaryItemService.selectByDicSn("source"));
        //查询交流方式下拉框数据(跟进 模态框 交流方式)
        model.addAttribute("communicationMethod",systemDictionaryItemService.selectByDicSn("communicationMethod"));

        model.addAttribute("pageInfo", customerService.query(qo));
        return "customer/lost_list";
    }

    /**
     * 处理 去增加或编辑客户页提交的
     */
    @RequiresPermissions(value = {"操作:新增或修改客户", "customer:saveOrUpdate"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer entity) {
        JsonResult jsonResult = new JsonResult();
        try {
            customerService.saveOrUpate(entity);
            return jsonResult;
        } catch (Exception e) {
            jsonResult.mark("服务器提示：操作客户出错");
            e.printStackTrace();
            return jsonResult;
        }

    }

    /**
     * 处理 修改客户状态
     */
    @RequiresPermissions(value = {"操作:修改客户状态", "customer:updateStatus"}, logical = Logical.OR)
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long customerId, Long status) {
        JsonResult jsonResult = new JsonResult();
        try {
            customerService.updateStatus(customerId, status);
            return jsonResult;
        } catch (Exception e) {
            jsonResult.mark("服务器提示：操作修改客户状态出错");
            e.printStackTrace();
            return jsonResult;
        }

    }

    /**
     * 处理 删除客户
     */
    @ResponseBody
    @RequiresPermissions(value = {"操作:删除客户", "customer:delete"}, logical = Logical.OR)
    @RequestMapping("/delete")
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            customerService.delete(id);
            return jsonResult;
        } catch (Exception e) {
            jsonResult.mark("服务器提示：删除客户操作出错");
            e.printStackTrace();
            return jsonResult;
        }

    }

}









































